<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>商品购物车</title>
    <link rel="shortcut icon" href="./../../favicon.ico">
    <link rel="stylesheet" href="../../style/cart/shoppingCart.css">
    <script type="text/javascript" src="./../../plugins/jQuery/jquery-3.6.0.js"></script>
    <script type="text/javascript" src="./../../plugins/vue/vue.js"></script>
    <script type="text/javascript" src="./../../plugins/axios/axios.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="./../../plugins/element-ui/index.css">
    <!-- 引入组件库 -->
    <script src="./../../plugins/element-ui/index.js"></script>
    <!-- 引入layer -->
    <script src="../../plugins/layer-master/dist/layer.js"></script>
    <style>
        .el-table {
            margin: 0 auto;
        }
    </style>
</head>
<body>
<div id="app">
    <nav class="public-header">
        <a href="../../index.html">网站首页</a>
        <a href="">我的订单</a>
        <a href="">我的地址</a>
    </nav>
    <div class="cart">
        <template>
            <el-table
                    ref="multipleTable"
                    :data="my_carts"
                    tooltip-effect="dark"
                    style="width: 87%"
                    @selection-change="getSelection"
            >
                <el-table-column
                        type="selection"
                        width="55"
                        align="center"
                >
                </el-table-column>
                <el-table-column
                        label="商品"
                        width="220"
                        align="center"
                >
                    <template slot-scope="scope">
                        <img width="120px" :src=`http:${scope.row.imageName}`>
                    </template>
                </el-table-column>
                <el-table-column
                        label="书名"
                        align="center"
                >
                    <template slot-scope="scope">
                        {{scope.row.name}}
                    </template>
                </el-table-column>
                <el-table-column
                        label="出版社"
                        align="center"
                >
                    <template slot-scope="scope">
                        {{scope.row.publisher}}
                    </template>
                </el-table-column>
                <el-table-column
                        prop="price"
                        label="单价"
                        width="120"
                        align="center"
                >
                    <template slot-scope="scope">￥{{ scope.row.price }}元</template>
                </el-table-column>
                <el-table-column
                        prop="number"
                        label="数量"
                        show-overflow-tooltip
                        align="center"
                        width="200px"
                >
                    <template slot-scope="scope">
                        <el-button size="mini" type="danger" @click="changeCatNumber1(scope.row)">-1</el-button>
                        <input type="text"
                               style="width: 50px; height: 23px"
                               v-model.number="scope.row.number"
                               @keyup="changeCatNumber3(scope.row)"
                        >
                        <el-button size="mini" type="success" @click="changeCatNumber2(scope.row)">+1</el-button>
                    </template>

                </el-table-column>
                <el-table-column
                        prop="address"
                        label="小计"
                        show-overflow-tooltip
                        align="center"
                        width="120"
                >
                    <template slot-scope="scope">￥{{ (scope.row.price * scope.row.number).toFixed(2) }}元</template>
                </el-table-column>
                <el-table-column
                        prop="address"
                        label="操作"
                        show-overflow-tooltip
                        align="center"
                        width="120"
                >
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                type="danger"
                                align="center"
                                @click="removeCart(scope.row)"
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-button type="danger" class="dangers" v-if="my_carts.length > 0" id="batchDeleteBtn"
                       @click="removeCartBySelection"
            >批量删除
            </el-button>
            <div class="cart-btn"><span>总价：</span><span> ¥{{totalPrice}} </span>
                <button class="btn" @click="toPay">去结算</button>
            </div>
        </template>
        <!-- 页脚部分 -->
        <div class="extra">
            <div class="wrapper">
                <!-- 版权信息 -->
                <div class="copyright">
                    <p>
                        <a href="javascript:;">关于我们</a>
                        <a href="javascript:;">帮助中心</a>
                        <a href="javascript:;">售后服务</a>
                        <a href="javascript:;">配送与验收</a>
                        <a href="javascript:;">商务合作</a>
                        <a href="javascript:;">搜索推荐</a>
                        <a href="javascript:;">友情链接</a>
                    </p>
                    <p>CopyRight &copy; 加中实训</p>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    new Vue({
        el: '#app',
        data() {
            return {
                my_carts: [],  // 我的购物车
                selection_carts: [],  // 选中的购物车
            }
        },
        methods: {
            // 加载我的购物车
            loadMyCart() {
                axios({
                    method: 'get',
                    url: 'http://localhost:8000/shopping_cart/find_cart_user',
                    headers: {
                        token: localStorage.getItem('front_token')
                    }
                }).then(resp => {
                    console.log(resp.data)
                    if (!resp.data.success) {
                        this.$message({
                            message: '请登录后访问购物车',
                            type: 'error',
                            duration: 2000,
                            onClose: () => {
                                location.href = '../login/login.html'
                            }
                        });
                        return
                    }
                    this.my_carts = resp.data.data
                })
            },
            // 购物车数量-1
            changeCatNumber1(row) {
                // 判断数量大于0，才允许减1
                if (row.number <= 0) {
                    this.$message({
                        message: '数量不能小于0',
                        type: 'error',
                        duration: 2000
                    });
                    return
                }
                let data = new URLSearchParams()
                data.append('book_id', row.id)
                axios({
                    method: 'put',
                    url: 'http://localhost:8000/shopping_cart/change_number_1',
                    data: data,
                    headers: {
                        token: localStorage.getItem('front_token')
                    }
                }).then(resp => {
                    if (resp.data.success) {
                        // 数量减1成功后，更新页面上的数量
                        row.number--
                    } else {
                        // 弹出失败的窗体
                        this.$message({
                            message: resp.data.message,
                            type: 'error',
                            duration: 2000
                        });
                    }
                })
            },
            // 购物车数量+1
            changeCatNumber2(row) {
                let data = new URLSearchParams()
                data.append('book_id', row.id)
                axios({
                    method: 'put',
                    url: 'http://localhost:8000/shopping_cart/change_number_2',
                    data: data,
                    headers: {
                        token: localStorage.getItem('front_token')
                    }
                }).then(resp => {
                    if (resp.data.success) {
                        // 数量+1成功后，更新页面上的数量
                        row.number++
                    } else {
                        // 弹出失败的窗体
                        this.$message({
                            message: resp.data.message,
                            type: 'error',
                            duration: 2000
                        });
                    }
                })
            },
            changeCatNumber3(row) {

                // 判断数量是否为0
                if (row.number <= 0) {
                    this.$message({
                        message: '数量不能小于0',
                        type: 'error',
                        duration: 2000
                    });
                    return
                }

                let data = new URLSearchParams()
                data.append('book_id', row.id)
                data.append('number', row.number)
                axios({
                    method: 'put',
                    url: 'http://localhost:8000/shopping_cart/change_number_3',
                    data: data,
                    headers: {
                        token: localStorage.getItem('front_token')
                    }
                }).then(resp => {
                    if (resp.data.success) {

                    } else {
                        // 弹出失败的窗体
                        this.$message({
                            message: resp.data.message,
                            type: 'error',
                            duration: 2000
                        });
                    }
                })
            },
            // 删除一个图书
            removeCart(row) {
                // 1. 询问是否删除
                this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // 2. 请求删除
                    axios({
                        method: 'delete',
                        url: 'http://localhost:8000/shopping_cart/remove_cart',
                        headers: {
                            token: localStorage.getItem('front_token')
                        },
                        params: {
                            book_id: row.id
                        }
                    }).then(resp => {
                        if (resp.data.success) {
                            // 3. 删除成功后，更新页面上的购物车列表
                            this.loadMyCart()
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        } else {
                            this.$message({
                                message: resp.data.message,
                                type: 'error',
                                duration: 2000
                            });
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },

            // 获取到选中的购物车
            getSelection(selectionItem) {
                this.selection_carts = selectionItem
            },
            // 批量删除
            removeCartBySelection() {
                console.log(1111)
                // 将对象集合转换成id字符串
                let ids = ""
                let books = this.selection_carts
                console.log(books)
                for (let i = 0; i < books.length; i++) {
                    ids += books[i].id + ','
                }
                console.log(ids)
                // 去掉最后一个逗号
                ids = ids.substring(0, ids.length - 1)
                this.$confirm('此操作将永久删除选中商品, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // 2. 请求删除
                    axios({
                        method: 'delete',
                        url: 'http://localhost:8000/shopping_cart/remove_cart_batch',
                        headers: {
                            token: localStorage.getItem('front_token')
                        },
                        params: {
                            book_ids: ids
                        }
                    }).then(resp => {
                        if (resp.data.success) {
                            this.loadMyCart()
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        } else {
                            this.$message({
                                message: resp.data.message,
                                type: 'error',
                                duration: 2000
                            });
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            toPay() {
                location.href = '../order/order.html'
            }
        },
        created() {
            this.loadMyCart()
        }
    })
</script>
</body>
</html>
